//
//  YWMaterialStockOutTableViewController.m
//  maintain
//
//  Created by 烟雨云渊 on 16/9/5.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWMaterialStockOutTableViewController.h"
#import "YWMaterialStockOutDetailTableViewController.h"
//#import "YWNoReadReplyTableViewController.h"
#import "YWMaterialRecordTableViewController.h"

#import "YWMaterialListCell.h"
#import "YWMaterialManager.h"
#import "YWMaterialApply.h"
#import "YWMaterialStockOut.h"
//#import "YWReplyManager.h"

#import "UIColor+CustomColors.h"
#import "UITableView+FDTemplateLayoutCell.h"
#import "NSDate+YYAdd.h"
#import "MJRefresh.h"
#import "YWCommonTool.h"
#import "YWMacro.h"
#import "UIColor+CustomColors.h"

@interface YWMaterialStockOutTableViewController ()
@property (nonatomic, strong) NSMutableArray * dateSource;
@property (nonatomic, strong) YWMaterialApplyViewModel * applyViewModel;
@property (nonatomic, assign) NSInteger page;
@end

@implementation YWMaterialStockOutTableViewController
//- (instancetype)init{
//    return [super initWithStyle:UITableViewStyleGrouped];
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 1;
    [self initTableView];
}

- (void)initTableView{
    [self.tableView registerClass:[YWMaterialListCell class] forCellReuseIdentifier:YWMaterialListCellIdentifier];
    self.tableView.backgroundColor = [UIColor customBackgroudColor];
    self.tableView.backgroundColor = [UIColor customBackgroudColor];
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, KbottomViewH, 0);
    self.tableView.estimatedRowHeight = 44.0f;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshTop)];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshFootor)];
    self.tableView.mj_footer.automaticallyHidden = YES;
    self.tableView.sectionFooterHeight = 0.01f;
    // 马上进入刷新状态
    [self.tableView.mj_header beginRefreshing];
    
}

- (void)refreshTop
{
    self.page = 1;
    [self.tableView.mj_footer resetNoMoreData];
    [self requestData:YWTableViewFreshTypeDown];
}

- (void)refreshFootor
{
    [self requestData:YWTableViewFreshTypeUp];
}

- (void)requestData:(YWTableViewFreshType)refreshType
{
    if (refreshType == YWTableViewFreshTypeUp) {
        self.page++;
    }
    YWWeakSelf(weakSelf);
    [weakSelf.tableView.mj_footer resetNoMoreData];
    [[YWMaterialManager shareManager] requestMaterialReceipientListInfoPage:weakSelf.page Status:YWMaterialStatusHaveStockout WithComplete:^(BOOL success) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (refreshType == YWTableViewFreshTypeDown) {
                [weakSelf.tableView.mj_header endRefreshing];
            }else {
                [weakSelf.tableView.mj_footer endRefreshing];
            }
            
            if (success) {
                weakSelf.dateSource = nil;
                [weakSelf.tableView reloadData];
            }
        });
    }];
}

- (NSMutableArray *)dateSource{
    if (!_dateSource) {
        _dateSource = [NSMutableArray array];
        [_dateSource addObjectsFromArray:[[YWMaterialManager shareManager] materialReceipientWithPage:self.page]];
    }
    return _dateSource;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return self.dateSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [self.dateSource[section] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YWMaterialListCell * cell = [tableView dequeueReusableCellWithIdentifier:YWMaterialListCellIdentifier];
    [self configureCell:cell atIndexPath:indexPath];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    YWMaterialStockOutDetailTableViewController * materialDetailController = [[YWMaterialStockOutDetailTableViewController alloc] init];
    YWMaterialRecordTableViewController * recordVC = [[YWMaterialRecordTableViewController alloc] init];
    YWMaterialStockOut * stockOut = self.dateSource[indexPath.section][indexPath.row];
//    materialDetailController.receipient = receipient;
    recordVC.stockOut = stockOut;
    [self.navigationController pushViewController:recordVC animated:YES];
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    return [tableView fd_heightForCellWithIdentifier:YWMaterialListCellIdentifier cacheByIndexPath:indexPath configuration:^(YWMaterialListCell *cell) {
//        [self configureCell:cell atIndexPath:indexPath];
//    }];
//}

- (void)configureCell:(YWMaterialListCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    cell.fd_enforceFrameLayout = NO;
    YWMaterialStockOut * receipient = self.dateSource[indexPath.section][indexPath.row];
    cell.recipient = receipient;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 35;
}

#pragma mark - table view section header view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    static NSString* HeaderIdentifier = @"YWMaterialStockOutTableViewControllerForHeaderInSection";
    UITableViewHeaderFooterView* headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HeaderIdentifier];
    if (headerView == nil) {
        headerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:HeaderIdentifier];
        headerView.contentView.backgroundColor = [UIColor customBackgroudColor];
        UILabel* label = [[UILabel alloc] init];
        label.textAlignment = NSTextAlignmentLeft;
        label.textColor = [UIColor blackColor];
        label.tag = 2016;
        label.font = KTitleFont;
        [headerView addSubview:label];
    }
    
    NSString *headText = [self titleForHeaderInSection:section];
    UILabel* label = [headerView viewWithTag:2016];
    label.text = headText;
    label.frame = CGRectMake(15, 0, SCREEN_WIDTH - 2 * 15, 35);
    return headerView;
}

- (NSString *)titleForHeaderInSection:(NSInteger)section{
    NSArray *array = [self.dateSource[section] count]>0?self.dateSource[section]:nil;
    YWMaterialStockOut * stockOut = array.count?array[0]:nil;
    NSString *time = [YWCommonTool showChineseFormatterDate:stockOut.stockOutTime/1000];
    NSInteger weekDay = [NSDate dateWithTimeIntervalSince1970:stockOut.stockOutTime/1000].weekday;
    NSString *weekStr = [YWCommonTool weekDayString:weekDay-1];
    NSString * title = [NSString stringWithFormat:@"%@  %@",time,weekStr];
    return title;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
